package com.ce.pms.custservice.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import junit.framework.Assert;

import com.ce.pms.DBUnitTool;
import com.ce.pms.custservice.dao.ICustServiceEventDao;
import com.ce.pms.custservice.dao.ISignReadingDao;
import com.ce.pms.custservice.model.CustServiceEventDO;
import com.ce.pms.custservice.model.SignReadingRecordDO;
import com.ce.pms.custservice.vo.CustServiceEventVO;
import com.ce.pms.custservice.vo.SignReadingRecordVO;

/***
 *
 * @author 高嵩
 *
 */
public class SignReadingServiceImplT extends DBUnitTool{

	private ISignReadingDao signReadingDao;
	private ICustServiceEventDao custServiceEventDao;
	private IEventManageService eventManageService;
	private ISignReadingService signReadingService;

	/* (non-Javadoc)
	 * @see com.ce.pms.DBUnitTool#setUp()
	 */
	@Override
	public void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		initSignReading();
		Assert.assertNotNull(eventManageService);
		Assert.assertNotNull(signReadingService);
		Assert.assertNotNull(signReadingDao);
		Assert.assertNotNull(custServiceEventDao);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.DBUnitTool#tearDown()
	 */
	@Override
	public void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
		signReadingDao = null;
		custServiceEventDao = null;
		eventManageService = null;
		signReadingService = null;
	}

	private void initSignReading(){
		if(this.eventManageService == null){
			this.eventManageService = (IEventManageService)this.applicationContext.getBean("eventManageService");
		}
		if(this.signReadingDao ==  null){
			this.signReadingDao = (ISignReadingDao)this.applicationContext.getBean("signReadingDao");
		}
		if(this.custServiceEventDao == null){
			this.custServiceEventDao = (ICustServiceEventDao)this.applicationContext.getBean("custServiceEventDao");
		}
		if(this.signReadingService == null){
			this.signReadingService = (ISignReadingService)this.applicationContext.getBean("signReadingService");
		}
	}
	private CustServiceEventVO getCustServiceEventVO(CustServiceEventVO custServEventVO){

		custServEventVO.setContact("联系人");
		custServEventVO.setEventContent("事件内容");
		custServEventVO.setValidity("001");//有效性:001有效;002待定;000无效
		custServEventVO.setCreator(new Long(1));//创建人
		//custServEventVO.setModifier(new Long(2));//修改人
		custServEventVO.setHouseKeyword(new Long(6));//房号主键

		custServEventVO.setEventRegionKeyword(new Long(2));//事件区域
		custServEventVO.setCustServEventKindKeyword(new Long(1));//事件类型
		custServEventVO.setEventNatureKeyword(new Long(1));//客户事件性质
		custServEventVO.setSeriousDegreeKeyword(new Long(1));//时间严重程度ID
		custServEventVO.setEmergencyDegreeKeyword(new Long(1));//紧急程度
		custServEventVO.setAppointment("2009-01-01");//预约时间
		return custServEventVO;
	}


		/***
	    * 功能描述:  阅签<br>
	    * @param csEventID
	    * @param signReading
	    * @roseuid 4928231B0213
	    * Create author:高嵩 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */

	/**
	 * 事件状态:已完成
	 */
	public static final String EVENT_STATUS_COMPLETE_CODE = "003";
	/**
	 * 阅签状态:待阅签
	 */
	public static final String REVIEWER_STATUZ_PREPARE_CODE = "001";

		@Test
	   public void addSignReading()
	   {
			System.out.println("----------------------------阅签测试开始--------------------------------------");

			SignReadingRecordVO signReadingRecordVO = new SignReadingRecordVO();
			signReadingRecordVO.setAdvice("advice");
			//审阅者
			signReadingRecordVO.setReviewer("审阅者");
			//创建人
			signReadingRecordVO.setCreator(new Long(1));

			CustServiceEventVO custServEventVO = new CustServiceEventVO();
//			增加客服事件记录
			this.eventManageService.addCustEvent(this.getCustServiceEventVO(custServEventVO));

			CustServiceEventDO custServiceEventDODB = this.custServiceEventDao.findById(custServEventVO.getKeyword());
			//增加成功
			assertEquals(custServEventVO.getKeyword(),custServiceEventDODB.getKeyword());

			//如果新建状态的事件 应该阅签不成功，
			//this.signReadingService.addSignReading(custServiceEventDODB.getKeyword(), signReadingRecordVO);


//			如果事件已完成，并且阅签状态为待阅签时，才可以阅签
//------------------------------------------------------------------------------------------------------------
			//设置状态为完成
			custServEventVO.setEventStatuz(EVENT_STATUS_COMPLETE_CODE);
			//设置状态为待阅签
			custServEventVO.setReviewerStatuz(REVIEWER_STATUZ_PREPARE_CODE);
			this.eventManageService.editCustEvent(custServEventVO);
			custServiceEventDODB = this.custServiceEventDao.findById(custServEventVO.getKeyword());
			assertEquals(custServiceEventDODB.getEventStatuz(),EVENT_STATUS_COMPLETE_CODE);
			assertEquals(custServiceEventDODB.getReviewerStatuz(),REVIEWER_STATUZ_PREPARE_CODE);
			//事件已完成，并且阅签状态为待阅签时,进行阅签,结果应该正确执行
			this.signReadingService.addSignReading(custServiceEventDODB.getKeyword(), signReadingRecordVO);

			//删除记录
			this.signReadingDao.deleteObjectById(signReadingRecordVO.getKeyword());
			Assert.assertNull(this.signReadingDao.findById(signReadingRecordVO.getKeyword()));
			this.custServiceEventDao.deleteObjectById(custServiceEventDODB.getKeyword());
			Assert.assertNull(this.custServiceEventDao.findById(custServiceEventDODB.getKeyword()));
//			------------------------------------------------------------------------------------------------------------
			System.out.println("----------------------------阅签测试end--------------------------------------");
	   }
}
